package com.tankgame.panel;

import com.tankgame.models.BaseTank;
import com.tankgame.models.PlayerTank;
import com.tankgame.server.TankClient;
import com.tankgame.server.TankMsg;
import com.tankgame.server.TankServer;
import com.tankgame.utils.Constants;
import com.tankgame.utils.Directions;
import com.tankgame.utils.UniqueTankVector;

import javax.swing.*;
import java.awt.*;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;


public class GamePanel extends JPanel implements KeyListener {
    private static GamePanel instance;
    private final Timer timer;
    private PlayerTank playerTank = null;
    private UniqueTankVector allTanks = new UniqueTankVector();
    private TankClient tankClient = null;

    public GamePanel() {
        //定时刷新
        timer = new Timer(Constants.GAME_SPEED, e -> repaint());
        timer.start();

        //创建一个玩家坦克
        this.playerTank = new PlayerTank(Constants.PLAYER_TANK_INITIAL_X, Constants.PLAYER_TANK_INITIAL_Y);
        this.playerTank.setSpeed(Constants.PLAYER_TANK_SPEED);
        allTanks.add(playerTank);

        //进行连接到服务器
        tankClient = new TankClient(allTanks, playerTank);
        tankClient.connect(TankServer.IP, TankServer.TCP_PORT);

//         创建敌方坦克
//        for (int i = 1; i <= 3; i++) {
//            EnemyTank enemyTank = new EnemyTank(Constants.TANK_WIDTH * 2 * i, Constants.TANK_HEIGHT);
//            enemyTank.setSpeed(Constants.ENEMY_TANK_SPEED);
//            enemyTank.setDirection(Directions.DOWN);
//            enemyTanks.add(enemyTank);
//            allTanks.add(enemyTank);
//            // 启动敌方坦克线程
//            new Thread(enemyTank).start();
//        }
    }
    //创建单例模式
    public static GamePanel GetInstance() {
        if (instance == null) {
//            System.out.println("create instance");
            instance = new GamePanel();
        }
        return instance;
    }

    public void addTankInAll(BaseTank tank) {
        allTanks.add(tank);
    }

    public UniqueTankVector getAllTanks() {
        return allTanks;
    }

    public void setAllTanks(UniqueTankVector allTanks) {
        this.allTanks = allTanks;
    }

    @Override
    protected void paintComponent(Graphics g) {
        super.paintComponent(g);
        Graphics2D g2d = (Graphics2D) g;
        g2d.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON);
    
        // 绘制背景
        g2d.setColor(Constants.GAME_PANEL_COLOR);
        g2d.fillRect(Constants.GAME_PANEL_X, Constants.GAME_PANEL_Y, Constants.GAME_PANEL_WIDTH,
                Constants.GAME_PANEL_HEIGHT);

        // 绘制玩家坦克
        for (BaseTank tank : allTanks) {
            //绘制坦克
            tank.draw(g2d);
            // 绘制玩家坦克的子弹
            tank.drawMissiles(g2d, allTanks);
        }

        // 绘制敌方坦克
//        for (BaseTank enemyTank : enemyTanks) {
//            enemyTank.draw(g2d);
//            enemyTank.drawMissiles(g2d, allTanks);
//        }
    }

    @Override
    public void keyTyped(KeyEvent e) {

    }

    @Override
    public void keyPressed(KeyEvent e) {
        Directions moveDirection = null;
        switch (e.getKeyCode()) {
            // 移动
            case KeyEvent.VK_UP:
            case KeyEvent.VK_W:
                moveDirection = Directions.UP;
//                System.out.println("上");
                break;
            case KeyEvent.VK_DOWN:
            case KeyEvent.VK_S:
                moveDirection = Directions.DOWN;
//                System.out.println("下");
                break;
            case KeyEvent.VK_LEFT:
            case KeyEvent.VK_A:
                moveDirection = Directions.LEFT;
//                System.out.println("左");
                break;
            case KeyEvent.VK_RIGHT:
            case KeyEvent.VK_D:
                moveDirection = Directions.RIGHT;
//                System.out.println("右");
                break;
            // 开火
            case KeyEvent.VK_SPACE:
            case KeyEvent.VK_ENTER:
            case KeyEvent.VK_J:
//                System.out.println("fire");
                playerTank.fire();
                break;
            // 端口面板
//            case KeyEvent.VK_C:
//                connDialog.setVisible(true);
//                System.out.println(connDialog.isDisplayable());
//                break;
        }
        if (moveDirection != null) {
            playerTank.setDirection(moveDirection);
            playerTank.move();

//            坦克移动发送消息
            TankMsg tankMoveMsg = new TankMsg(playerTank);
            tankClient.send(tankMoveMsg);
        }
        repaint();
    }

    @Override
    public void keyReleased(KeyEvent e) {

    }

//    class ConnDialog extends JDialog {
//        Button btn = new Button("commit");
//        TextField ipField = new TextField("127.0.0.1", 12);
//        TextField portField = new TextField("" + TankServer.TCP_PORT, 4);
//        TextField udpField = new TextField("1000", 4);
//        public ConnDialog() {
//            super((Frame) SwingUtilities.getWindowAncestor(GamePanel.this), true);
//            this.setLayout(new FlowLayout());
//
//            this.add(new Label("IP:"));
//            this.add(ipField);
//            this.add(new Label("Port:"));
//            this.add(portField);
//            this.add(new Label("My UDP_PORT"));
//            this.add(udpField);
//            this.add(btn);
//            this.pack();
////            this.setVisible(true);
//
////            this.setLocation(300, 300);
//            this.setLocationRelativeTo(null);
//            this.addWindowListener(new WindowAdapter() {
//                @Override
//                public void windowClosing(java.awt.event.WindowEvent e) {
//                    setVisible(false);
//                }
//            });
//            //解析输入进来的ip, port 以及端口号
//            btn.addActionListener(new AbstractAction() {
//                @Override
//                public void actionPerformed(ActionEvent e) {
//                    if(e.getSource() != btn) return;
//                    String ip = ipField.getText().trim();
////                    int port = Integer.parseInt(portField.getText().trim());
////                    int udpPort = Integer.parseInt(udpField.getText().trim());
////                    tankClient.setUdpPort(udpPort);
//                    tankClient.connect(ip, playerTank.getId());
//                    setVisible(false);
//                }
//            });
//        }
//    }
}
